<?php
require_once ('../helper/config.inc.php');
class Controller{

    public $_controller;
    public $_view;

    public function __construct()
    {

        session_start();
        $smarty= new User_Smarty();
        $this->smarty = $smarty;
    }

    public function getSmarty()
    {
        return $this->smarty;
    }

    public function dd($data){
        echo '<pre>';
        print_r($data);
        echo '</pre>';
        die();
    }

    public function view($url,$data){
        $samrty = $this->getSmarty();

//        $data = [
//            'username' => [
//                'id' => '12',
//                'ip' => '13',
//            ],
//        ];
//
//        $this->dd($data);
        $samrty->assign('data',$data);
        $samrty->display($url);
        exit();
    }

    /*
     * render html with data
     */
    function render_to_response($template_file='',$context=array() )
    {
//	die(print_r($context));
        $a = false;
        $content = array();
        if($template_file == '')
        {
            return;
        }
        $tpl = new User_Smarty();
//	echo 2;print_r($tpl);die();

        foreach($context as $key => $v)
        {
            if(is_array($v)){
                $content[] = render_to_response($template_file,$v);

            }else{
                $a = true;
                //$tpl->assign($key,$v);
                $content[$key] = $v;
            }
        }
        if($a)

            //return $content;
            $tpl->assign('content',$content);

        $encoding = defined('ENCODEING') ?ENCODEING:'UTF-8';
        header("Content-Type: text/html; charset=$encoding");

        $tpl->display($template_file);
        exit;
    }

    /*
     * 302:redirect
     */
    function redirect($url){
        header("Location: ".$url);
        exit;
    }
}